define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Salesoutlets,GoDialog, $window, $timeout, $ionicSideMenuDelegate,GoAddress) {
		//数据模型
        $scope.scopeModel = new Object();
        $scope.scopeModel.region_id = "";
        $scope.scopeModel.brand_id ="";
        $scope.scopeModel.brand_select = {id:0,brand_name:"请选择品牌"};
        if($scope.input == undefined) $scope.input = new Object();
        $scope.input.nPage = 1;
        
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			//动态设置高度
			var all = $window.innerHeight;
			//计算滚动条
			$scope.myObj = {
				"height": all - 44 + "px"
			};
		}, false);
		$scope.doSelectBrand = function(){
			var list = new Array();
			$scope.brands.forEach(obj => {
				if(obj.id == undefined)
					obj.id = obj.brand_id
				list.push(obj);
			});
			//id是值 name是显示
			GoDialog.showSelect("品牌",list,$scope.scopeModel.brand_select,function(data){
				$scope.scopeModel.brand_id = data.brand_id;
				$scope.scopeModel.brand_select = data;
			});
		}
				//加载数据
		$scope.init_data = function() {
		  	Salesoutlets.getlist($scope.scopeModel.region_id,$scope.scopeModel.brand_id,5,1,function(data) {
            	$scope.detail = data;
            });
            Salesoutlets.brands(function(data) {
            	if(data!=null){
            		$scope.brands = data.brands;
            	}
            });
		}
		$scope.init_data(); //必须在定义之后

		
		$scope.isLoad = false; //可以加载
		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			//防止重复加载
			$scope.get_region();
			$scope.isLoad = true;
			Salesoutlets.getlist($scope.scopeModel.region_id,$scope.scopeModel.brand_id,5,1,function(data) {
				if(data!=null){
					$scope.detail = data;
				}else{
					$scope.detail = new Object();					
				}
				$scope.input.nPage = 1;
			},
			function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
				$scope.isLoad = false;
			}, 1500);
			});
		};
		//加载更多
		$scope.doRefreshMore = function() {
			//防止重复加载
			$scope.isLoad = true;
			$scope.get_region();
			Salesoutlets.getlist($scope.scopeModel.region_id,$scope.scopeModel.brand_id,5,$scope.input.nPage+1,function(data) {
				if(data!=null){
					if($scope.detail!=undefined){
						$scope.detail.outlet = $scope.detail.outlet.concat(data.outlet);
						$scope.input.nPage += 1;
					}
					else{
						$scope.detail = data;
					}	
				}
			},
			function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
				$scope.isLoad = false;
			}, 1500);
			
			});	
		};
		//侧边菜单
		$scope.rightSideMenu = function() {
			$ionicSideMenuDelegate.toggleRight();
		};
	
		//查询
		$scope.doSearch = function() {
			$scope.get_region();
			Salesoutlets.getlist($scope.scopeModel.region_id,$scope.scopeModel.brand_id,5,1,function(data){
				$scope.detail = data;
			});
			$ionicSideMenuDelegate.toggleRight();
		};
		//取消
		$scope.doCancel = function() {
			$ionicSideMenuDelegate.toggleRight();
		};
		/***省市区三级联动***/
		$scope.input.province = {id:0,name:"请选择省份"}; //省市区三级联动
		$scope.input.city = {id:0,name:"请选择城市"}; //省市区三级联动
		$scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
		$scope.doSelectProvince = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择省份",$scope.province_items,$scope.input.province,function(data){
				$scope.input.province = data;
			});
		}
		$scope.doSelectCity = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择城市",$scope.city_items,$scope.input.city,function(data){
				$scope.input.city = data;
			});
		}
		$scope.doSelectDistrict = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择区/县",$scope.district_items,$scope.input.district,function(data){
				$scope.input.district = data;
			});
		}
		//下拉侦听，目前只能省市区三级联动
		$scope.listen_province = function (data) {
			$scope.input.city = {id:0,name:"请选择城市"}; //省市区三级联动
			$scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
			$scope.district_items = null;
		};
		$scope.listen_city = function (data) {
			$scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
		};
		//定义绑定
		var items = new Array();
		var obj1 = { name: 'input.province' };
		var obj2 = { name: 'input.city' };
		var obj3 = { name: 'input.district' };
		items.push(obj1);
		items.push(obj2);
		items.push(obj3);
		GoAddress.init_load($scope, items);
		$scope.get_region = function() {
			$scope.scopeModel.region_id = '';
			if($scope.input.province != null){
				$scope.scopeModel.region_id = $scope.input.province.id
			}
			if($scope.input.city != null){
				$scope.scopeModel.region_id = $scope.input.city.id
			}
			if($scope.input.district != null) {
				$scope.scopeModel.region_id = $scope.input.district.id;
			}
		}
		
		
	}

	controller.$inject = ['$scope', 'Salesoutlets','GoDialog', '$window', '$timeout','$ionicSideMenuDelegate','GoAddress'];

	return app.registerController("salesoutletsctrl",controller);
});